﻿using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Collections.Generic;
using System.Web.Caching;
using System.Web;
using System.IO;
using System;


namespace ACService
{
    [ServiceContract(Namespace = "JsonpAjaxService")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service
    {
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        public List<string> GetWords()
        {
            List<string> responseList = new List<string>();
            string value = HttpContext.Current.Request.QueryString["parametr"];
            if (HttpContext.Current.Cache[value] == null)
            {
                using (StreamReader file = new StreamReader(HttpContext.Current.Server.MapPath("English words.txt")))
                {
                    while (true)
                    {
                        string line = file.ReadLine();
                        if (line == null) break;
                        if ((line.StartsWith(value)))
                            responseList.Add(line);
                    }
                }

                if (responseList.Count != 0)
                    HttpContext.Current.Cache.Insert(value, responseList, null, DateTime.Now.AddSeconds(30), Cache.NoSlidingExpiration);

                return responseList;
            }
            else return (List<string>)HttpContext.Current.Cache[value];
        }
    }
}